<?php


class Model_Agent extends PhalApi_Model_NotORM
{
	// 我的邀请码
	public function getCode($uid)
	{
		$codeDb    = DI()->notorm->agent_code;
		$agentinfo = $codeDb->select('code')->where('uid=?', $uid)->fetchOne();
		// 如果不存在 则生成
		if(empty($agentinfo)){
			$code = createCode();
			$codeDb->insert(array('uid' => $uid, "code" => $code));
			
			$agentinfo = $codeDb->select('code')->where('uid=?', $uid)->fetchOne();
		}
		
		return $agentinfo;
	}
	
	// 我的上级
	public function agent($uid)
	{
		$agentinfo   = [];
		$users_agent = DI()->notorm->agent->where('uid=?', $uid)->fetchOne();
		if($users_agent){
			$agentinfo = getUserInfo($users_agent['one_uid']);
			// 代理上的邀请码
			$agent_code = DI()->notorm->agent_code->where("uid=?", $users_agent['one_uid'])->fetchOne();
			
			$agentinfo['code'] = $agent_code['code'];
		}else{
			$agentinfo = (object)[];
		}
		
		return $agentinfo;
	}
	
	
	// 我的收益
	public function profit($uid)
	{
		$one_profit  = 0;
		$agentprofit = DI()->notorm->agent_profit->where('uid=?', $uid)->fetchOne();
		if($agentprofit){
			$one_profit = $agentprofit['one_profit'];
		}
		
		return $one_profit;
	}
	
	// 设置我的上级
	public function setAgent($uid, $code)
	{
		// 查找自己是否已经设置
		$isexist = DI()->notorm->agent->where('uid=?', $uid)->fetchOne();
		if($isexist){
			return 1001;  // 已设置
		}
		
		// 获取邀请码的用户信息
		$oneinfo = DI()->notorm->agent_code->where('code=?', $code)->fetchOne();
		if(!$oneinfo){
			return 1002;  // 邀请码错误
		}
		if($oneinfo['uid'] == $uid){
			return 1003; // 不能填写自己的邀请码
		}
		
		// 查找该邀请码的父级
		$one_agent = DI()->notorm->agent->where('uid=?', $oneinfo['uid'])->fetchOne();
		if(!$one_agent){
			$one_agent = array('uid' => $oneinfo['uid'], 'one_uid' => 0,);
		} else{
			// 填写的父级的父级不能是自己
			if($one_agent['one_uid'] == $uid){
				return 1004;  // 您已经是该用户的上级
			}
		}
		
		// 保存
		$data = array('uid' => $uid, 'one_uid' => $one_agent['uid'], 'addtime' => time(),);
		DI()->notorm->agent->insert($data);
		
		
		return $data;
	}
	
	// 退出代理
	public function quit($uid)
	{
		DI()->notorm->agent->where('uid=?', $uid)->delete();
		
		return true;
	}
	
	// 代理收益
	public function getProfit($uid, $p, $pnum = 50)
	{
		if($p < 1){
			$p = 1;
		}
		
		$start = ($p - 1) * $pnum;
		$list  = DI()->notorm->agent_profit_recode->select("uid,one_profit as profit, addtime")->where('one_uid=?', $uid)
												  ->order('addtime desc')->limit($start, $pnum)->fetchAll();
		foreach($list as $k => $v){
			$list[$k]['userinfo'] = getUserInfo($v['uid']);
			$list[$k]['profit']   = NumberFormat($v['profit']);
		}
		
		
		return $list;
	}
	
	// 汇总代理收益
	public function sumProfit($uid)
	{
		// 获取总收益
		$tTime = strtotime(date("Y-m-d"));
		$total = DI()->notorm->agent_profit_recode->select("sum(one_profit) as profit_all")->where('one_uid=?', $uid)->fetchOne();
		$today = DI()->notorm->agent_profit_recode->select("sum(one_profit) as profit_all")->where('one_uid=? and addtime>= ? and addtime<? ', $uid, $tTime, $tTime + 86400)->fetchOne();
		// 组合
		$total = ['total' => $total['profit_all'] ?? "0", 'today' => $today['profit_all'] ?? "0",];
		
		return $total;
	}
	
	public function totalProfit($uid, $p, $pnum = 50)
	{
		if($p < 1){
			$p = 1;
		}
		
		$start = ($p - 1) * $pnum;
		$list  = DI()->notorm->agent_profit_recode
			->select("uid,sum(one_profit) as total")
			->where('one_uid=?', $uid)->order('addtime desc')->group("uid")->limit($start, $pnum)
			->fetchAll();
		foreach($list as $k => $v){
			$list[$k]['userinfo'] = getUserInfo($v['uid']);
			$list[$k]['total']    = NumberFormat($v['total']);
		}
		
		return $list;
	}
	
	
}
